package com.example.eos.service;

import com.example.eos.bean.Comment;
import com.example.eos.bean.ReturnInfo;
import com.example.eos.bean.User;
import com.example.eos.mapper.CommentMapper;
import com.example.eos.mapper.PublishMapper;
import com.example.eos.tool.GlobalParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    private CommentMapper commentMapper;
    private PublishMapper publishMapper;

    @Autowired
    public CommentService(CommentMapper commentMapper, PublishMapper publishMapper){
        this.commentMapper = commentMapper;
        this.publishMapper = publishMapper;
    }

    //查看评论
    public ReturnInfo getComment(String commentid){
        if(commentMapper.findByCommentid(commentid)==null){
            return ReturnInfo.info2("失败","");
        }else {
            return ReturnInfo.info2("成功",commentMapper.findByCommentid(commentid));
        }
    }

    //查看评论列表
    public ReturnInfo getComments(String publishid){
        List<Comment> list = commentMapper.findByPublishid(publishid);
        return ReturnInfo.info2("成功",list);
    }

    //新建评论
    public ReturnInfo saveComment(Comment comment, List<String> stringList){
        if(publishMapper.findByPublishid(comment.getPublishid())==null){
            stringList.add("作品不存在");
            return ReturnInfo.info2("失败", stringList);
        }else {
            comment.setCommentid(GlobalParam.getUUID());
            comment.setReplynum(0);
            comment.setCmttime(GlobalParam.getTime());
            User user =(User) SecurityUtils.getSubject().getPrincipal();
            comment.setUserid(user.getUserid());
            commentMapper.save(comment);
            publishMapper.updateCommentnum(comment.getPublishid());
            return ReturnInfo.info2("成功",stringList);
        }
    }

    //删除评论
    public ReturnInfo deteleComment(String commentid){
        if(commentMapper.findByCommentid(commentid)==null){
            return ReturnInfo.info1("失败");
        }else {
            commentMapper.deleteByCommentid(commentid);
            return ReturnInfo.info1("成功");
        }
    }

}
